Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable/Immutable refactor and GetImmutable snapshots #92

Merged
merged 14 commits into from
Aug 11, 2018

Conversation

UnitylChaos
Copy link
Contributor

@UnitylChaos UnitylChaos commented Jul 26, 2018

This fixes #88 and #68, with a side effect of resolving #50 and #87.

@UnitylChaos UnitylChaos changed the title WIP - Mutable/Immutable refactor and GetImmutable snapshots Mutable/Immutable refactor and GetImmutable snapshots Jul 31, 2018
@liamsi
Copy link
Contributor

liamsi commented Jul 31, 2018

Are we still able to run the benchmark scripts with these changes? Could you do so and report how the performance / mem-consumption changed? (e.g., does moving the ref to the db from tree to node impact mem-consumption on large trees?)

@liamsi liamsi added T:enhancement Type: Enhancement T:breaking Type: Breaking Change labels Jul 31, 2018
Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @jlandrews. I think this separates concerns and clarifies the API.
I left a few minor comments (mostly documentation nits).

It would be good to have someone's input who uses the API a lot (or is deeply familiar with the previous design decisions), too. /CC @silasdavis @jaekwon

version int64
}

// NewImmutableTree creates both in-memory and persistent instances
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation nit: this sounds like NewImmutableTree always creates both. Could be changed to:

NewImmutableTree creates an ImmutableTree.
If a database is provided as an argument, it will be used for persistence, otherwise an in-memory instance will be created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just delete this function actually. If a user creates a new ImmutableTree with this method, they won't be able to set any data on it. It's currently not being called from anywhere in this repo or in the SDK code either.

}
return t.root.height
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should keep differently typed "getters" for the same data (Height8 / Height & Version Version64 & Get / Get64 & GetByIndex / GetByIndex64).


// IterateRange makes a callback for all nodes with key between start and end non-inclusive.
// If either are nil, then it is open on that side (nil, nil is the same as Iterate)
func (t *ImmutableTree) IterateRange(start, end []byte, ascending bool, fn func(key []byte, value []byte) bool) (stopped bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a sentence about the return value (currently stopped)? E.g.: "It returns true if ... " Also, we might remove the named return and just change it to:
IterateRange(start, end []byte, ascending bool, fn func(key []byte, value []byte) bool) bool
I think this is slightly more general.


// IterateRangeInclusive makes a callback for all nodes with key between start and end inclusive.
// If either are nil, then it is open on that side (nil, nil is the same as Iterate)
func (t *ImmutableTree) IterateRangeInclusive(start, end []byte, ascending bool, fn func(key, value []byte, version int64) bool) (stopped bool) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit confusing that IterateRangeInclusive takes a version while IterateRange does not.

mutable_tree.go Outdated
// TODO: optimize balance & rotate.
node = node.clone(version)
l := node.getLeftNode()
_l := l.clone(version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename _l to newNode and l to orphaned? Maybe we can remove the named returns here, too (func (tree *MutableTree) rotateRight(node *Node) (*Node, *Node) instead)

mutable_tree.go Outdated
// TODO: optimize balance & rotate.
node = node.clone(version)
r := node.getRightNode()
_r := r.clone(version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.


// DeleteVersion deletes a tree version from disk. The version can then no
// longer be accessed.
func (tree *MutableTree) DeleteVersion(version int64) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure: this is what you called DeleteImmutable(version) in your proposal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I changed my mind about renaming it. Seemed like it made more sense to say we're deleting an old version of the mutable tree, rather than that we're deleting an immutable tree.

Copy link
Contributor

@liamsi liamsi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge this and I can update the comments afterwards in separate PRs, too.

@UnitylChaos UnitylChaos force-pushed the jlandrews/immutable branch 2 times, most recently from d1f292f to c1f6d4e Compare August 1, 2018 01:29
@UnitylChaos
Copy link
Contributor Author

So I've run the "fullbench" benchmarks (which took more than 32gb of ram, so we might want to trim them down a bit), and it seems like performance is quite similar to develop.

That doesn't account for actual tree size differences though, and I think I was wrong to add the nodeDB reference to each node, since it's a waste of memory to have it on each one. I only did that because I thought it was nicer to have node.getLeftNode() and node.getRightNode() without arguments of the tree. That's not a necessary change though, so I've reverted the particular commit that did it.

@alexanderbez
Copy link
Contributor

Don't have too much context here, but the changes and discussion look very clean and concise. Does any relevant documentation need to be updated (i.e. readme, docs, etc...)? Also, @jlandrews, do you think the benchmarks should also be run with --benchmem and compare against the base branch to see improvements?

@UnitylChaos
Copy link
Contributor Author

UnitylChaos commented Aug 1, 2018

Reran with -benchmem and compared results. Overall it looks pretty similar, but there are some metrics that changed by 10-20% either direction. Not sure how to interpret it though, since it seems like these tests are non-deterministic and so there may be some variance anyway...

Expand to see benchmark results
benchmark                                                                 old ns/op     new ns/op     delta
BenchmarkRandomBytes/random-4-24                                          72.0          74.8          +3.89%
BenchmarkRandomBytes/random-16-24                                         118           121           +2.54%
BenchmarkRandomBytes/random-32-24                                         156           166           +6.41%
BenchmarkRandomBytes/random-100-24                                        370           376           +1.62%
BenchmarkRandomBytes/random-1000-24                                       2958          2943          -0.51%
BenchmarkSmall/memdb-1000-100-4-10/query-miss-24                          4420          4081          -7.67%
BenchmarkSmall/memdb-1000-100-4-10/query-hits-24                          5272          5112          -3.03%
BenchmarkSmall/memdb-1000-100-4-10/update-24                              159192        157089        -1.32%
BenchmarkSmall/memdb-1000-100-4-10/block-24                               24824001      25654741      +3.35%
BenchmarkSmall/goleveldb-1000-100-4-10/query-miss-24                      6368          7127          +11.92%
BenchmarkSmall/goleveldb-1000-100-4-10/query-hits-24                      8741          8713          -0.32%
BenchmarkSmall/goleveldb-1000-100-4-10/update-24                          109113        103509        -5.14%
BenchmarkSmall/goleveldb-1000-100-4-10/block-24                           17755475      17394312      -2.03%
BenchmarkSmall/leveldb-1000-100-4-10/query-miss-24                        6584          6214          -5.62%
BenchmarkSmall/leveldb-1000-100-4-10/query-hits-24                        7898          8254          +4.51%
BenchmarkSmall/leveldb-1000-100-4-10/update-24                            102305        107058        +4.65%
BenchmarkSmall/leveldb-1000-100-4-10/block-24                             16187510      17031744      +5.22%
BenchmarkMedium/memdb-100000-100-16-40/query-miss-24                      9595          10064         +4.89%
BenchmarkMedium/memdb-100000-100-16-40/query-hits-24                      10834         11143         +2.85%
BenchmarkMedium/memdb-100000-100-16-40/update-24                          1261394       1303374       +3.33%
BenchmarkMedium/memdb-100000-100-16-40/block-24                           198937291     190258294     -4.36%
BenchmarkMedium/goleveldb-100000-100-16-40/query-miss-24                  22673         22452         -0.97%
BenchmarkMedium/goleveldb-100000-100-16-40/query-hits-24                  28135         28301         +0.59%
BenchmarkMedium/goleveldb-100000-100-16-40/update-24                      294852        296013        +0.39%
BenchmarkMedium/goleveldb-100000-100-16-40/block-24                       34807659      35855483      +3.01%
BenchmarkMedium/leveldb-100000-100-16-40/query-miss-24                    22686         22802         +0.51%
BenchmarkMedium/leveldb-100000-100-16-40/query-hits-24                    27668         31757         +14.78%
BenchmarkMedium/leveldb-100000-100-16-40/update-24                        287108        297615        +3.66%
BenchmarkMedium/leveldb-100000-100-16-40/block-24                         35596044      36791150      +3.36%
BenchmarkLarge/memdb-1000000-100-16-40/query-miss-24                      15911         15781         -0.82%
BenchmarkLarge/memdb-1000000-100-16-40/query-hits-24                      15748         15750         +0.01%
BenchmarkLarge/memdb-1000000-100-16-40/update-24                          5161409       5256647       +1.85%
BenchmarkLarge/memdb-1000000-100-16-40/block-24                           514923704     534785952     +3.86%
BenchmarkLarge/goleveldb-1000000-100-16-40/query-miss-24                  60866         59997         -1.43%
BenchmarkLarge/goleveldb-1000000-100-16-40/query-hits-24                  49123         51637         +5.12%
BenchmarkLarge/goleveldb-1000000-100-16-40/update-24                      478900        476117        -0.58%
BenchmarkLarge/goleveldb-1000000-100-16-40/block-24                       53327657      56340657      +5.65%
BenchmarkLarge/leveldb-1000000-100-16-40/query-miss-24                    57290         62528         +9.14%
BenchmarkLarge/leveldb-1000000-100-16-40/query-hits-24                    48691         50966         +4.67%
BenchmarkLarge/leveldb-1000000-100-16-40/update-24                        381530        456299        +19.60%
BenchmarkLarge/leveldb-1000000-100-16-40/block-24                         56220875      58929008      +4.82%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-miss-24        23263         20237         -13.01%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-hits-24        25201         26440         +4.92%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/update-24            283769        276849        -2.44%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/block-24             33455540      33371134      -0.25%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-miss-24       23683         23127         -2.35%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-hits-24       32174         30518         -5.15%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/update-24           317281        317968        +0.22%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/block-24            44231625      40372117      -8.73%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-miss-24      35564         34792         -2.17%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-hits-24      45093         45317         +0.50%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/update-24          574978        521571        -9.29%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/block-24           76693083      78074233      +1.80%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-miss-24     147724        160833        +8.87%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-hits-24     169279        172494        +1.90%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/update-24         3882880       3285298       -15.39%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/block-24          520453137     482757364     -7.24%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/query-miss-24         22097         22448         +1.59%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/query-hits-24         27734         32738         +18.04%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/update-24             511317        515236        +0.77%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/block-24              2681891       2702588       +0.77%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/query-miss-24        23397         23648         +1.07%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/query-hits-24        28137         28103         -0.12%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/update-24            349234        351358        +0.61%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/block-24             9405218       9918965       +5.46%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/query-miss-24       22684         24165         +6.53%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/query-hits-24       28836         29367         +1.84%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/update-24           279935        291198        +4.02%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/block-24            34924252      35941442      +2.91%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/query-miss-24       21974         22196         +1.01%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/query-hits-24       29079         28725         -1.22%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update-24           200319        200024        -0.15%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/block-24            143250792     152324790     +6.33%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/query-miss-24      22070         22555         +2.20%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/query-hits-24      28305         28427         +0.43%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/update-24          150990        143810        -4.76%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/block-24           416917935     408154340     -2.10%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-miss-24        19841         20109         +1.35%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-hits-24        25320         25620         +1.18%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/update-24            310418        276745        -10.85%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/block-24             33174907      33831408      +1.98%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-miss-24       23820         23057         -3.20%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-hits-24       28973         28850         -0.42%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/update-24           315650        310652        -1.58%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/block-24            41392373      40865126      -1.27%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-miss-24      33961         33848         -0.33%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-hits-24      45861         43692         -4.73%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/update-24          594813        588636        -1.04%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/block-24           77458855      82922740      +7.05%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-miss-24     148305        147862        -0.30%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-hits-24     169529        178420        +5.24%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/update-24         3540659       3606703       +1.87%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/block-24          515252342     459105626     -10.90%

benchmark                                                                 old allocs     new allocs     delta
BenchmarkRandomBytes/random-4-24                                          1              1              +0.00%
BenchmarkRandomBytes/random-16-24                                         1              1              +0.00%
BenchmarkRandomBytes/random-32-24                                         1              1              +0.00%
BenchmarkRandomBytes/random-100-24                                        1              1              +0.00%
BenchmarkRandomBytes/random-1000-24                                       1              1              +0.00%
BenchmarkSmall/memdb-1000-100-4-10/query-miss-24                          9              9              +0.00%
BenchmarkSmall/memdb-1000-100-4-10/query-hits-24                          12             12             +0.00%
BenchmarkSmall/memdb-1000-100-4-10/update-24                              764            763            -0.13%
BenchmarkSmall/memdb-1000-100-4-10/block-24                               120912         120940         +0.02%
BenchmarkSmall/goleveldb-1000-100-4-10/query-miss-24                      13             13             +0.00%
BenchmarkSmall/goleveldb-1000-100-4-10/query-hits-24                      18             18             +0.00%
BenchmarkSmall/goleveldb-1000-100-4-10/update-24                          254            253            -0.39%
BenchmarkSmall/goleveldb-1000-100-4-10/block-24                           39158          39338          +0.46%
BenchmarkSmall/leveldb-1000-100-4-10/query-miss-24                        14             13             -7.14%
BenchmarkSmall/leveldb-1000-100-4-10/query-hits-24                        18             18             +0.00%
BenchmarkSmall/leveldb-1000-100-4-10/update-24                            254            254            +0.00%
BenchmarkSmall/leveldb-1000-100-4-10/block-24                             39097          39144          +0.12%
BenchmarkMedium/memdb-100000-100-16-40/query-miss-24                      10             10             +0.00%
BenchmarkMedium/memdb-100000-100-16-40/query-hits-24                      12             12             +0.00%
BenchmarkMedium/memdb-100000-100-16-40/update-24                          4746           4746           +0.00%
BenchmarkMedium/memdb-100000-100-16-40/block-24                           795945         795943         -0.00%
BenchmarkMedium/goleveldb-100000-100-16-40/query-miss-24                  27             26             -3.70%
BenchmarkMedium/goleveldb-100000-100-16-40/query-hits-24                  35             35             +0.00%
BenchmarkMedium/goleveldb-100000-100-16-40/update-24                      592            594            +0.34%
BenchmarkMedium/goleveldb-100000-100-16-40/block-24                       67622          67658          +0.05%
BenchmarkMedium/leveldb-100000-100-16-40/query-miss-24                    25             27             +8.00%
BenchmarkMedium/leveldb-100000-100-16-40/query-hits-24                    35             35             +0.00%
BenchmarkMedium/leveldb-100000-100-16-40/update-24                        593            594            +0.17%
BenchmarkMedium/leveldb-100000-100-16-40/block-24                         67118          67963          +1.26%
BenchmarkLarge/memdb-1000000-100-16-40/query-miss-24                      20             20             +0.00%
BenchmarkLarge/memdb-1000000-100-16-40/query-hits-24                      15             15             +0.00%
BenchmarkLarge/memdb-1000000-100-16-40/update-24                          20570          20570          +0.00%
BenchmarkLarge/memdb-1000000-100-16-40/block-24                           2069282        2069277        -0.00%
BenchmarkLarge/goleveldb-1000000-100-16-40/query-miss-24                  82             82             +0.00%
BenchmarkLarge/goleveldb-1000000-100-16-40/query-hits-24                  62             62             +0.00%
BenchmarkLarge/goleveldb-1000000-100-16-40/update-24                      836            838            +0.24%
BenchmarkLarge/goleveldb-1000000-100-16-40/block-24                       98883          95529          -3.39%
BenchmarkLarge/leveldb-1000000-100-16-40/query-miss-24                    76             81             +6.58%
BenchmarkLarge/leveldb-1000000-100-16-40/query-hits-24                    60             61             +1.67%
BenchmarkLarge/leveldb-1000000-100-16-40/update-24                        736            841            +14.27%
BenchmarkLarge/leveldb-1000000-100-16-40/block-24                         101281         100887         -0.39%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-miss-24        27             26             -3.70%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-hits-24        34             34             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/update-24            563            565            +0.36%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/block-24             64612          65264          +1.01%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-miss-24       28             28             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-hits-24       37             37             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/update-24           616            616            +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/block-24            73233          73717          +0.66%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-miss-24      30             30             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-hits-24      39             39             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/update-24          752            695            -7.58%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/block-24           92942          93850          +0.98%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-miss-24     64             65             +1.56%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-hits-24     70             71             +1.43%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/update-24         639            621            -2.82%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/block-24          102744         84213          -18.04%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/query-miss-24         27             25             -7.41%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/query-hits-24         35             35             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/update-24             842            843            +0.12%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/block-24              4350           4385           +0.80%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/query-miss-24        26             27             +3.85%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/query-hits-24        35             35             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/update-24            704            703            -0.14%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/block-24             18575          18689          +0.61%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/query-miss-24       27             27             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/query-hits-24       35             35             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/update-24           595            594            -0.17%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/block-24            67455          67049          -0.60%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/query-miss-24       26             26             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/query-hits-24       36             35             -2.78%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update-24           454            448            -1.32%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/block-24            273493         273242         -0.09%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/query-miss-24      26             26             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/query-hits-24      35             35             +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/update-24          326            320            -1.84%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/block-24           824207         815927         -1.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-miss-24        24             26             +8.33%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-hits-24        34             34             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/update-24            564            566            +0.35%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/block-24             64856          64847          -0.01%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-miss-24       27             28             +3.70%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-hits-24       37             37             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/update-24           618            617            -0.16%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/block-24            74022          74541          +0.70%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-miss-24      30             30             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-hits-24      39             39             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/update-24          760            754            -0.79%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/block-24           92146          93883          +1.89%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-miss-24     63             63             +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-hits-24     71             72             +1.41%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/update-24         640            640            +0.00%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/block-24          102385         89339          -12.74%

benchmark                                                                 old bytes     new bytes     delta
BenchmarkRandomBytes/random-4-24                                          4             4             +0.00%
BenchmarkRandomBytes/random-16-24                                         16            16            +0.00%
BenchmarkRandomBytes/random-32-24                                         32            32            +0.00%
BenchmarkRandomBytes/random-100-24                                        112           112           +0.00%
BenchmarkRandomBytes/random-1000-24                                       1024          1024          +0.00%
BenchmarkSmall/memdb-1000-100-4-10/query-miss-24                          435           434           -0.23%
BenchmarkSmall/memdb-1000-100-4-10/query-hits-24                          634           633           -0.16%
BenchmarkSmall/memdb-1000-100-4-10/update-24                              42771         42683         -0.21%
BenchmarkSmall/memdb-1000-100-4-10/block-24                               6620344       6619026       -0.02%
BenchmarkSmall/goleveldb-1000-100-4-10/query-miss-24                      649           637           -1.85%
BenchmarkSmall/goleveldb-1000-100-4-10/query-hits-24                      918           918           +0.00%
BenchmarkSmall/goleveldb-1000-100-4-10/update-24                          22326         22232         -0.42%
BenchmarkSmall/goleveldb-1000-100-4-10/block-24                           3487423       3483478       -0.11%
BenchmarkSmall/leveldb-1000-100-4-10/query-miss-24                        651           646           -0.77%
BenchmarkSmall/leveldb-1000-100-4-10/query-hits-24                        918           919           +0.11%
BenchmarkSmall/leveldb-1000-100-4-10/update-24                            22352         22312         -0.18%
BenchmarkSmall/leveldb-1000-100-4-10/block-24                             3461634       3482495       +0.60%
BenchmarkMedium/memdb-100000-100-16-40/query-miss-24                      513           513           +0.00%
BenchmarkMedium/memdb-100000-100-16-40/query-hits-24                      676           676           +0.00%
BenchmarkMedium/memdb-100000-100-16-40/update-24                          246830        246834        +0.00%
BenchmarkMedium/memdb-100000-100-16-40/block-24                           40016809      40016520      -0.00%
BenchmarkMedium/goleveldb-100000-100-16-40/query-miss-24                  1594          1539          -3.45%
BenchmarkMedium/goleveldb-100000-100-16-40/query-hits-24                  2144          2148          +0.19%
BenchmarkMedium/goleveldb-100000-100-16-40/update-24                      53115         52887         -0.43%
BenchmarkMedium/goleveldb-100000-100-16-40/block-24                       5968256       6213133       +4.10%
BenchmarkMedium/leveldb-100000-100-16-40/query-miss-24                    1532          1595          +4.11%
BenchmarkMedium/leveldb-100000-100-16-40/query-hits-24                    2159          2147          -0.56%
BenchmarkMedium/leveldb-100000-100-16-40/update-24                        53026         52713         -0.59%
BenchmarkMedium/leveldb-100000-100-16-40/block-24                         6206475       6289507       +1.34%
BenchmarkLarge/memdb-1000000-100-16-40/query-miss-24                      1061          1061          +0.00%
BenchmarkLarge/memdb-1000000-100-16-40/block-24                           100084344     100083320     -0.00%
BenchmarkLarge/goleveldb-1000000-100-16-40/query-miss-24                  4902          4900          -0.04%
BenchmarkLarge/goleveldb-1000000-100-16-40/query-hits-24                  3745          3748          +0.08%
BenchmarkLarge/goleveldb-1000000-100-16-40/update-24                      81376         81887         +0.63%
BenchmarkLarge/goleveldb-1000000-100-16-40/block-24                       10397199      10034120      -3.49%
BenchmarkLarge/leveldb-1000000-100-16-40/query-miss-24                    4590          5003          +9.00%
BenchmarkLarge/leveldb-1000000-100-16-40/query-hits-24                    3640          3701          +1.68%
BenchmarkLarge/leveldb-1000000-100-16-40/update-24                        69914         86644         +23.93%
BenchmarkLarge/leveldb-1000000-100-16-40/block-24                         10656291      12193146      +14.42%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-miss-24        1745          1659          -4.93%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-hits-24        2238          2248          +0.45%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/update-24            52149         52649         +0.96%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/block-24             5749534       5881967       +2.30%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-miss-24       2716          2732          +0.59%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-hits-24       3716          3739          +0.62%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/update-24           64047         63822         -0.35%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/block-24            7399545       7424951       +0.34%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-miss-24      11875         11953         +0.66%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-hits-24      16763         16693         -0.42%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/update-24          207200        189390        -8.60%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/block-24           28530852      26628664      -6.67%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-miss-24     256119        257161        +0.41%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-hits-24     307666        312459        +1.56%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/update-24         2601694       2404346       -7.59%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/block-24          431258147     364325902     -15.52%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/query-miss-24         1601          1530          -4.43%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/query-hits-24         2157          2172          +0.70%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/update-24             78187         78273         +0.11%
BenchmarkLevelDBBatchSizes/goleveldb-100000-5-16-40/block-24              398000        402931        +1.24%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/query-miss-24        1536          1598          +4.04%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/query-hits-24        2163          2160          -0.14%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/update-24            64083         65065         +1.53%
BenchmarkLevelDBBatchSizes/goleveldb-100000-25-16-40/block-24             1702630       1711946       +0.55%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/query-miss-24       1609          1620          +0.68%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/query-hits-24       2156          2154          -0.09%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/update-24           52609         53061         +0.86%
BenchmarkLevelDBBatchSizes/goleveldb-100000-100-16-40/block-24            5994318       6034383       +0.67%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/query-miss-24       1568          1579          +0.70%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/query-hits-24       2212          2154          -2.62%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/update-24           40108         40108         +0.00%
BenchmarkLevelDBBatchSizes/goleveldb-100000-400-16-40/block-24            23631405      23616529      -0.06%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/query-miss-24      1543          1538          -0.32%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/query-hits-24      2150          2159          +0.42%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/update-24          31752         30211         -4.85%
BenchmarkLevelDBBatchSizes/goleveldb-100000-2000-16-40/block-24           78008320      78403509      +0.51%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-miss-24        1591          1668          +4.84%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/query-hits-24        2237          2243          +0.27%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/update-24            51051         52750         +3.33%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100/block-24             5796150       5826982       +0.53%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-miss-24       2638          2722          +3.18%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/query-hits-24       3720          3742          +0.59%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/update-24           64815         63335         -2.28%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-1000/block-24            7424452       7547879       +1.66%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-miss-24      11884         12084         +1.68%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/query-hits-24      16535         16592         +0.34%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/update-24          213379        211307        -0.97%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-10000/block-24           28589214      29007420      +1.46%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-miss-24     252620        250440        -0.86%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/query-hits-24     310811        313624        +0.91%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/update-24         2632158       2569932       -2.36%
BenchmarkLevelDBLargeData/goleveldb-50000-100-32-100000/block-24          440797695     401755606     -8.86%

@alexanderbez
Copy link
Contributor

Really neat work @jlandrews 👍

@@ -220,7 +220,7 @@ func TestIntegration(t *testing.T) {
}

records := make([]*record, 400)
tree := NewTree(nil, 0)
tree := NewMutableTree(db.NewMemDB(), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posting what I said in the call here for future reference:

Anywhere in the tests where we were using Tree mutably I had to change it to MutableTree, and if you pass nil as the db to MutableTree it errors when constructing the nodedb. Using MutableTree with a MemDB as nodedb is equivalent to the old code using Tree.

@UnitylChaos
Copy link
Contributor Author

I added back the ability to load old versions into the MutableTree so that integration into the SDK was easier. I also have a branch on the SDK set up to use the new API, so we can switch it over pretty easily once this is finalized.

mutable_tree.go Outdated
latestRoot = r
}
}

if !(targetVersion == 0 || latestVersion == targetVersion) {
return latestVersion, fmt.Errorf("Wanted to load target %v but only found up to %v",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: error messages are usually non-capitalized in golang: https://github.com/golang/go/wiki/CodeReviewComments#error-strings

@@ -39,67 +39,48 @@ func (t *Tree) String() string {
}

// Size returns the number of leaf nodes in the tree.
func (t *Tree) Size() int {
func (t *ImmutableTree) Size() int {
return int(t.Size64())
Copy link
Contributor

@liamsi liamsi Aug 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have mentioned this already somewhere but won't all this casting to int cause funky behaviour on 32 bit machines? Unrelated to this PR but I think we should just keep the *64 version of all these methods.

@liamsi
Copy link
Contributor

liamsi commented Aug 6, 2018

Thanks for adding the orphan test and addressing the comments! This still looks good to me. What could be improved: Can we have a test that shows the change in behaviour of SaveVersion in 3d7299f ?

Can you remind me if there was sth. else then the orphan test that we agreed should be included into this PR?

@UnitylChaos
Copy link
Contributor Author

So in the current code, it's possible to load any version of the tree, but you cannot overwrite previously saved versions. To enforce this, the library loads all roots into memory even when trying to load a specific version. Then when saving, it checks if version n+1 already exists in memory, and if so it compares the hashes and only tells the caller it was a successful save if the hash of the existing tree matches the hash of the intended tree. The original refactor I made removed this functionality entirely, since I thought it would make more sense to only load the current mutable "head" of the tree and completely disallow loading old versions mutably.
This causes a problem in the SDK though, since we're loading old versions and replaying updates in tests, as well as for gaiadebug where we want to load old versions directly into the app. Rather than change all of that code to load the current version and use snapshots, I figured it would be easier to add back in the ability to load old versions of the tree as though they were mutable, and block overwrites by querying the db for the root hash of that version at save time.
There are tests which cover this behavior in the SDK through baseapp, but I can make a simpler one here.

I also can't think of anything else we wanted to include in this PR...

@liamsi
Copy link
Contributor

liamsi commented Aug 6, 2018

There are tests which cover this behavior in the SDK through baseapp, but I can make a simpler one here.

That would awesome and then let's merge this.

@liamsi
Copy link
Contributor

liamsi commented Aug 7, 2018

Thanks @jlandrews! Great work! Let's see if @jaekwon has anything to add to this. I'll merge soon otherwise.

@liamsi liamsi mentioned this pull request Aug 7, 2018

// GetImmutable loads an ImmutableTree at a given version for querying
func (tree *MutableTree) GetImmutable(version int64) (*ImmutableTree, error) {
rootHash := tree.ndb.getRoot(version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the leaks are gone, and we get a sensible global cache behaviour of the node cache

@silasdavis
Copy link
Contributor

I think this is a great change - it opens up some flexible ways of using the library and tidies up various things. Storing tree versions as a set map[int64]bool and fetching previous roots from the DB removes the leaks I was worrying about and is cleaner than unloading previous trees. Hope this will end up in a release forthwith and I shall upgrade!

@liamsi
Copy link
Contributor

liamsi commented Aug 10, 2018

Hope this will end up in a release forthwith and I shall upgrade!

Thanks for chiming in @silasdavis, we'll merge this very soon. I'll put together a release shortly after.

@liamsi liamsi merged commit da94e6c into develop Aug 11, 2018
liamsi added a commit that referenced this pull request Aug 15, 2018
* dep: Change tendermint dep to be ^v0.22.0 (#91)

* Mutable/Immutable refactor and GetImmutable snapshots (#92)

* Release 0.10.0: Update Changelog and bump version (#99)

See changelog: https://github.com/tendermint/iavl/blob/develop/CHANGELOG.md#0100
danil-lashin pushed a commit to danil-lashin/iavl that referenced this pull request Oct 24, 2018
* 'develop' of github.com:danil-lashin/iavl:
  Prep Release 0.11.0 (cosmos#111)
  Remove architecture dependent getter functions (cosmos#96)
  Use 8 bytes to store int64 components of database keys  (cosmos#107)
  Update to CircleCI 2.0 (cosmos#108)
  Release 0.10.0: Update Changelog and bump version (cosmos#99)
  delete empty file (relict from merging master into develop)
  Mutable/Immutable refactor and GetImmutable snapshots (cosmos#92)
  Remove unreachable code
  Remove unused variable
  dep: Change tendermint dep to be ^v0.22.0 (cosmos#91)
  Fix benchmark scripts for current code (cosmos#89)
  release v0.9.2 (cosmos#82)
  Various lints (cosmos#80)
  Jae/rangeprooffix (cosmos#75)
ridenaio pushed a commit to idena-network/iavl that referenced this pull request Jul 1, 2019
* Move orphaningTree logic into VersionedTree

* Move Tree.Set and Node.set to VersionedTree, fix tests

* Move Tree.Remove and node.remove to VersionedTree

* Rename VersionedTree/Tree to MutableTree/ImmutableTree

* Rename files to match type names

* Add GetImmutable and lazy loading of MutableTree

* Move balance and rotate from Node to MutableTree

* Add benchmarks with -benchmem, remove incomplete benchmark

* Rename variables in rotation functions

* Add test to check for consistent tracking of orphans

* Add back ability to load old versions with idempotent saves

* Add idempotent save test case
ridenaio pushed a commit to idena-network/iavl that referenced this pull request Jul 1, 2019
* dep: Change tendermint dep to be ^v0.22.0 (cosmos#91)

* Mutable/Immutable refactor and GetImmutable snapshots (cosmos#92)

* Release 0.10.0: Update Changelog and bump version (cosmos#99)

See changelog: https://github.com/tendermint/iavl/blob/develop/CHANGELOG.md#0100
@tac0turtle tac0turtle deleted the jlandrews/immutable branch July 11, 2019 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T:breaking Type: Breaking Change T:enhancement Type: Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants